﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Sub : System.Web.UI.Page
    {
        string[] parasValue = new string[] { };
        protected void Page_Load(object sender, EventArgs e)
        {
            //
            if (!Page.IsPostBack)
            {
               
                parasValue = new string[] { ddlSubName.Text,ddlSubYear.Text,SubFlag().ToString() };
                DataTable dt = new BLL.Sub().Search(parasValue);   
                Session["MaxPage"] = new BLL.Sub().MaxPage(parasValue);
                InfoBind(dt,int.Parse(Session["MaxPage"].ToString()));
                Session["SearchArr"] = parasValue;
                
                Session["Page"] = 1;

                ddlSubYear.DataSource = new BLL.Sub().GetSubYears();
                ddlSubYear.DataTextField = "sub_years";
                ddlSubYear.DataBind();
                ddlSubYear.Items.Insert(0, new ListItem("", ""));

                ddlSubName.DataSource = new BLL.Sub().GetSubName();
                ddlSubName.DataTextField = "sub_name";
                ddlSubName.DataBind();
                ddlSubName.Items.Insert(0, new ListItem("", ""));

                //DataTable dt = new BLL.Sub().GetSub();
                //this.infolist.DataSource = dt;
                //infolist.DataBind();
            }
           
        }


        public void InfoBind(DataTable dt ,int maxPage)
        {
            List<string> pageList = new  List<string>();
            for(int i=1; i<=maxPage; i++)
            {
                pageList.Add(i.ToString() + "/" + maxPage.ToString());
            }
            ddlPage.DataSource = pageList;
            ddlPage.DataBind();
            this.infolist.DataSource =dt;
            this.infolist.DataBind();
        }



        /// <summary>
        /// 增加事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {

          
            try
            {
                MODEL.Sub sub = new MODEL.Sub();
                sub.SubName = txtTempSubName.Text;
                sub.SubYears = int.Parse(txtTempSubYears.Text);
                sub.SubDesc = txtTempSubDesc.Text;
                int result = new BLL.Sub().Add(sub);
                if (result == 1)
                {
                    ddlSubYear.DataSource = new BLL.Sub().GetSubYears();
                    ddlSubYear.DataTextField = "sub_years";
                    ddlSubYear.DataBind();
                    ddlSubYear.Items.Insert(0, new ListItem("", ""));
                    Session["MaxPage"] = new BLL.Sub().MaxPage((string[])Session["SearchArr"]);
                    InfoBind(new BLL.Sub().Search((string[])Session["SearchArr"], int.Parse(Session["Page"].ToString())),
                        int.Parse(Session["MaxPage"].ToString()));
                }
                else
                {

                    ClientScript.RegisterStartupScript(this.GetType(), "Prompt", "<script type='text/javascript'>alert('添加失败')</script>");
                   // ClientScript.RegisterStartupScript(this.GetType(), "prompt", "$('#pPrompt').html('你好');$('#dialogPrompt').dialog('open');");
                }
            }
            catch
            {
                
            }
            //if (subName.Text == "" || subYear.Text == "" || subDesc.Text == "")
            //{
            //    Response.Write("<script type='text/javascript'>alert('请输入所有信息');</script>");
            //    return;
            //}

            //    MODEL.Sub sub = new MODEL.Sub();
            //    sub.SubName = subName.Text;
            //    sub.SubYears = Convert.ToInt32(subYear.Text);
            //    sub.SubDesc = subDesc.Text;

            //    int result = new BLL.Sub().Add(sub);
            //    if (result == 1)
            //    {
            //        Response.Write("<script type='text/javascript'>alert('增加成功');</script>");
            //    }
            //    subName.Text = null;
            //    subYear.Text = null;
            //    subDesc.Text = null;

            //    DataTable dt = new BLL.Sub().GetSub();
            //    this.infolist.DataSource = dt;
            //    infolist.DataBind();           
            
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            parasValue = new string[] {   ddlSubName.Text, ddlSubYear.Text, SubFlag().ToString() };
            DataTable dt = new BLL.Sub().Search(parasValue);
            Session["MaxPage"] = new BLL.Sub().MaxPage(parasValue);
            InfoBind(dt, int.Parse(Session["MaxPage"].ToString()));
            Session["SearchArr"] = parasValue;

            Session["Page"] = 1;

            //MODEL.Sub sub = new MODEL.Sub();
            //sub.SubName = subName.Text;
            //sub.SubYears = Convert.ToInt32(subYear.Text);

            //DataTable result = new BLL.Sub().GetName(sub);
            //this.infolist.DataSource = result;
            //infolist.DataBind();
        }

        /// <summary>
        /// 修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAlter_Click(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //DataTable dt = new BLL.Sub().GetSub();
            //this.infolist.DataSource = dt;
            //infolist.DataBind();

            //subName.Text = null;
            //subYear.Text = null;
            //subDesc.Text = null;
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int result = new BLL.Sub().DelId(int.Parse(this.txtTempSubID.Text));
            if (result == 1)
            {
                ddlSubYear.DataSource = new BLL.Sub().GetSubYears();
                ddlSubYear.DataTextField = "sub_years";
                ddlSubYear.DataBind();
                ddlSubYear.Items.Insert(0, new ListItem("", ""));
                Session["MaxPage"] = new BLL.Sub().MaxPage((string[])Session["SearchArr"]);
                InfoBind(new BLL.Sub().Search((string[])Session["SearchArr"], int.Parse(Session["Page"].ToString())),
                    int.Parse(Session["MaxPage"].ToString()));
            }
            else
            {

                ClientScript.RegisterStartupScript(this.GetType(), "Prompt", "<script type='text/javascript'>alert('删除失败')</script>");
                // ClientScript.RegisterStartupScript(this.GetType(), "prompt", "$('#pPrompt').html('你好');$('#dialogPrompt').dialog('open');");
            }
            //if (subName.Text == "" || subYear.Text == "")
            //{
            //    Response.Write("<script type='text/javascript'>alert('请输入要删除的年级和学制');</script>");
            //    return;
            //}

            //MODEL.Sub sub = new MODEL.Sub();
            //sub.SubName = subName.Text;
            //sub.SubYears = Convert.ToInt32(subYear.Text);


            //int result = new BLL.Sub().DelId(sub);
            //if (result == 1)
            //{
            //    Response.Write("<script type='text/javascript'>alert('删除成功');</script>");
            //}

            //DataTable dt = new BLL.Sub().GetSub();
            //this.infolist.DataSource = dt;
            //infolist.DataBind();

            //subName.Text = null;
            //subYear.Text = null;
            //subDesc.Text = null;
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            InfoBind(new BLL.Sub().Search(parasValue, page), int.Parse(Session["MaxPage"].ToString()));
            Session["page"] = page;
            this.ddlPage.SelectedIndex = page - 1;
        }




        public int SubFlag()
        {
            if (rbtnUse.Checked == true)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        protected void btnAFirst_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            InfoBind(new BLL.Sub().Search(parasValue), int.Parse(Session["MaxPage"].ToString()));
            Session["page"] = 1;
            this.ddlPage.SelectedIndex = 0;
        }

        protected void btnAPrevious_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int count = int.Parse(Session["page"].ToString()) - 1;
            if (count > 0)
            {
                InfoBind(new BLL.Sub().Search(parasValue, count), int.Parse(Session["MaxPage"].ToString()));
                Session["page"] = count;
                this.ddlPage.SelectedIndex = count - 1;
            }

        }

        protected void btnANext_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int count = int.Parse(Session["Page"].ToString()) + 1;
            int maxPage = int.Parse(Session["MaxPage"].ToString());
            if (count <= maxPage)
            {
                InfoBind(new BLL.Sub().Search(parasValue, count), maxPage);
                Session["page"] = count;
                this.ddlPage.SelectedIndex = count - 1;
            }
        }

        protected void btnALast_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int maxPage = int.Parse(Session["MaxPage"].ToString());
            InfoBind(new BLL.Sub().Search(parasValue, maxPage), maxPage);
            Session["page"] = maxPage;
            this.ddlPage.SelectedIndex = maxPage - 1;
        }

    }
}